﻿using Nop.Core.Domain.Catalog;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SpotsHub.Tools.MassUploader.Forms
{
    public partial class ProductAttribute : Form
    {
        public string ProductNameString;
        public List<string> ProductAttributes;
        public Dictionary<string, string> NewProductAttributes = new Dictionary<string, string>();

        //public List<string> ProductAttributeValues = new List<string>();

        public AttributeControlType SelectedControlType = AttributeControlType.DropdownList;
        private Dictionary<string, AttributeControlType> controlTypes = new Dictionary<string, AttributeControlType>();

        public string SelectedProductAttribute;

        public DataTable ProductAttributeValues;

        public bool IsRequired = false;

        public ProductAttribute()
        {
            InitializeComponent();
        }

        private void ProductAttribute_Load(object sender, EventArgs e)
        {
            controlTypes.Add("Checkboxes", AttributeControlType.Checkboxes);
            controlTypes.Add("DropdownList", AttributeControlType.DropdownList);
            controlTypes.Add("RadioList", AttributeControlType.RadioList);
            controlTypes.Add("TextBox", AttributeControlType.TextBox);


            ProductAttributeValues = new DataTable();
            ProductAttributeValues.Columns.Add("ValueName", typeof(string));
            ProductAttributeValues.Columns.Add("IsPreSelected", typeof(bool));
            ProductAttributeValues.Columns.Add("Order", typeof(int));
            ProductAttributeValues.Columns.Add("PriceAdjustment", typeof(decimal));
            ProductAttributeValues.Columns.Add("WeightAdjustment", typeof(decimal));
            dataGridView1.DataSource = ProductAttributeValues;

            label1.Text = String.Format("Edit product attributes for '{0}'", ProductNameString);
            ProductAttributes.ForEach(pa => comboBox1.Items.Add(pa));
            comboBox2.DataSource = controlTypes.Keys.ToList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(textBox1.Text))
            {
                return;
            }
            string att = textBox1.Text.Trim();
            if (!ProductAttributes.Contains(att))
            {
                if (MessageBox.Show(String.Format("Do you want to add '{0}' as the product attribute?", att), "Confirm", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    NewProductAttributes.Add(att, textBox2.Text.Trim());
                    comboBox1.Items.Add(att);
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //values = (DataTable)dataGridView1.DataSource;
            int pcount = 0;
            foreach (DataRow r in ProductAttributeValues.Rows)
	        {
                bool? v = r["IsPreSelected"] as bool?;

                if (v == null)
                {
                    r["IsPreSelected"] = false;
                }
                if (((bool?)r["IsPreSelected"]).Value)
                    pcount++;
	        }

            SelectedProductAttribute = comboBox1.SelectedItem as string;

            if (pcount > 1)
            {
                MessageBox.Show("Only one preffered value can be selected.");
            }
            else if (String.IsNullOrWhiteSpace(SelectedProductAttribute))
            {
                MessageBox.Show("Product attribute name is invalid.");
            }
            else
            {
                IsRequired = checkBox1.Checked;
                
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
    }
}
